package com.orange.lockscreen.layers.tip;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.orange.lockscreen.R;
import com.orange.lockscreen.SlatHashMap;
import com.orange.lockscreen.common.Constants.Slider_Msg;
import com.orange.lockscreen.layers.SlatPanel;

/**
 * 
 * TipSlatPanel: show prompt info when swipe or single tap on one slat.
 * 
 */
public class TipSlatPanel extends SlatPanel implements Slider_Msg,
		AnimationListener {

	// private static final String TAG = "TipSlatPanel";

	private static final int	AUTO_DISAPPEAR		= 1;
	private static final int	AUTO_SHOW			= 2;
	private static final int	TIME_AUTO_DISAPPEAR	= 600;

	private static final int	WHITH_COLOR_100		= 0xFFFFFFFF;

	private TextView			mTipTextView;

	private int					mRowIndex			= 0;

	private H					mH;

	private Animation			mAlphaAnimation;

	private boolean				mIsAnimating		= false;

	public TipSlatPanel(Context context) {
		super(context);
	}

	@Override
	protected void init() {
		mH = new H();

		mAlphaAnimation = AnimationUtils.loadAnimation(getContext(),
				R.anim.anim);
		mAlphaAnimation.setAnimationListener(this);

		mTipTextView = new TextView(getContext());
		// add by gaoge 2011-08-26 for fix bug outofmemoryError
		mTipTextView.setCursorVisible(false);
		mTipTextView.setText(R.string.tip_unlock);
		mTipTextView.setTextSize(15.0f);
		mTipTextView.setTypeface(Typeface.SANS_SERIF);
		mTipTextView.setTextColor(WHITH_COLOR_100);
		mTipTextView.setBackgroundResource(R.drawable.toast_frame);
		mTipTextView.setGravity(Gravity.CENTER_HORIZONTAL);
		addView(mTipTextView);

		mTipTextView.setVisibility(INVISIBLE);
	}

	public void show(int rowIndex, int resId, int delay) {
		mRowIndex = rowIndex;
		if (mTipTextView != null) {
			mTipTextView.setText(resId);
			boolean hadDisappearMsg = mH.hasMessages(AUTO_DISAPPEAR);
			if (hadDisappearMsg) {
				mH.removeMessages(AUTO_DISAPPEAR);
			}

			if (mIsAnimating) {
				mTipTextView.clearAnimation();
			}

			mTipTextView.setVisibility(INVISIBLE);

			hide();

			mH.sendEmptyMessageDelayed(AUTO_SHOW, delay);
		}
	}

	public void hide() {
		boolean hadShowMsg = mH.hasMessages(AUTO_SHOW);
		if (hadShowMsg) {
			mH.removeMessages(AUTO_SHOW);
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {

		if (mTipTextView != null && mTipTextView.getVisibility() != GONE) {
			int rowTop = SlatHashMap.getSlatTop(mRowIndex);
			int rowHeight = SlatHashMap.getSlatHeight(mRowIndex);
			int measuredWidth = mTipTextView.getMeasuredWidth();
			int measuredHeight = mTipTextView.getMeasuredHeight();
			int childLeft = (int) ((r - l - measuredWidth) / 2.0f);
			int childRight = childLeft + measuredWidth;
			int childTop = (int) (rowTop + (rowHeight - measuredHeight) / 2.0f) + 5;
			if (mRowIndex == SlatHashMap.UNLOCK_ROWINDEX) {
				int topOffset = (int) getContext().getResources().getDimension(
						R.dimen.main_unlock_tiptext_topoffset);
				childTop += topOffset;
			}
			int childBottom = childTop + measuredHeight;
			mTipTextView.layout(childLeft, childTop, childRight, childBottom);
		}
	}

	class H extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case AUTO_DISAPPEAR:
				if (mTipTextView != null) {
					mTipTextView.startAnimation(mAlphaAnimation);
				}
				break;
			case AUTO_SHOW:
				if (mTipTextView != null) {
					mTipTextView.setVisibility(VISIBLE);
					invalidate();
					mH.sendEmptyMessageDelayed(AUTO_DISAPPEAR,
							TIME_AUTO_DISAPPEAR);
				}
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		mIsAnimating = false;
		if (mTipTextView != null) {
			mTipTextView.setVisibility(INVISIBLE);
		}
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}

	@Override
	public void onAnimationStart(Animation animation) {
		mIsAnimating = true;
	}
}
